package com.quesucede.gameoflife;

import android.content.Context;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

/**
 * A fragment that provides an interface with which to update and
 * receive information from the settings XML
 * @author Brett Kromkamp, William Sugarman, and Brian Pokrifka
 *
 */
public class SettingsFragment extends PreferenceFragment {

	/**
	 * The underpopulation variable. Cells with too few neighbors die
	 */
	private static final String OPTION_MINIMUM = "UNDERPOPULATION_VARIABLE";
	
	/**
	 * Underpopulation's default value
	 */
	private static final String OPTION_MINIMUM_DEFAULT = "2";
	
	/**
	 * The overpopulation variable. Cells with too many neighbors die
	 */
	private static final String OPTION_MAXIMUM = "OVERPOPULATION_VARIABLE";
	
	/**
	 * Overpopulation's default value
	 */
	private static final String OPTION_MAXIMUM_DEFAULT = "3";
	
	/**
	 * The spawn variable. Cells with the right amount of neighbors live again
	 */
	private static final String OPTION_SPAWN = "SPAWN_VARIABLE";
	
	/**
	 * Spawn's default value
	 */
	private static final String OPTION_SPAWN_DEFAULT = "3";
	
	/**
	 * The speed of the Game of Life animation
	 */
	private static final String OPTION_ANIMATION_SPEED = "ANIMATION_SPEED_VARIABLE";
	
	/**
	 * The speed's default value
	 */
	private static final String OPTION_ANIMATION_SPEED_DEFAULT = "3";
	
	/**
	 * The color coding boolean that indicates whether to color code the cells by age
	 */
	private static final String OPTION_COLOR_CODING = "COLOR_CODING";
	
	/**
	 * Color coding's default value
	 */
	private static final boolean OPTION_COLOR_CODING_DEFAULT = false;
	
	@Override
	/**
	 * Initializes the settings from the XML
	 * @param savedInstanceState - the saved state
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);
	}
	
	/**
	 * Returns the underpopulation variable
	 * @param context - the current context
	 * @return the underpopulation variable
	 */
	public static String getMinimumVariable(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context).
			getString(OPTION_MINIMUM, OPTION_MINIMUM_DEFAULT);
	}
	
	/**
	 * Returns the overpopulation variable
	 * @param context - the current context
	 * @return the overpopulation variable
	 */
	public static String getMaximumVariable(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context).
			getString(OPTION_MAXIMUM, OPTION_MAXIMUM_DEFAULT);
	}
	
	/**
	 * Returns the spawn variable
	 * @param context - the current context
	 * @return the spawn variable
	 */
	public static String getSpawnVariable(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context).
			getString(OPTION_SPAWN, OPTION_SPAWN_DEFAULT);
	}
	
	/**
	 * Returns the animation speed
	 * @param context - the current context
	 * @return the animation speed
	 */
	public static String getAnimationSpeed(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context).
			getString(OPTION_ANIMATION_SPEED, OPTION_ANIMATION_SPEED_DEFAULT);
	}
	
	/**
	 * Returns whether or not we should color code
	 * @param context - the current context
	 * @return the color coding boolean
	 */
	public static boolean getColorCoding(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context).
			getBoolean(OPTION_COLOR_CODING, OPTION_COLOR_CODING_DEFAULT);
	}
}